package com.renge.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.renge.commonutils.R;
import com.renge.eduservice.entity.EduTeacher;
import com.renge.eduservice.entity.query.TeacherQuery;
import com.renge.eduservice.service.EduCourseService;
import com.renge.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-05-16
 */
@Api(description="讲师管理")
@RestController
@CrossOrigin
@RequestMapping("/eduservice/edu-teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;
    @Autowired
    private EduCourseService eduCourseService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R findAll(){
        List<EduTeacher> list = eduTeacherService.list(null);
        if(list.size()>0&&list!=null){
            return  R.ok().data("items",list);
        }else {
            return  R.error();
        }
    }
    @ApiOperation(value = "根据id查询讲师")
    @GetMapping("{id}")
    public R findOne(@ApiParam(name = "id",value = "讲师id",required = true)
                     @PathVariable("id")String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        if(!StringUtils.isEmpty(eduTeacher)){
          return   R.ok().data("eduTeacher",eduTeacher);
        }else {
          return  R.error();
        }
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeById(@ApiParam(name = "id", value = "讲师ID", required = true)
                                  @PathVariable("id")String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        boolean b = eduTeacherService.removeById(id);
        if (b){
           return R.ok();
        }else {
          return   R.error();
        }
    }

    @ApiOperation(value = "无条件分页查询")
    @GetMapping("{current}/{pagesize}")
    public R getByPage(@ApiParam(name = "current",value = "当前页",required = true)
                       @PathVariable("current")Integer current,
                       @ApiParam(name = "pagesize",value = "数据数量",required = true)
                       @PathVariable("pagesize")Integer pagesize){
        //new page
        Page<EduTeacher> page=new Page<>(current,pagesize);
        //根据条件分页
        IPage<EduTeacher> iPage = eduTeacherService.page(page, null);
        //判断返回是否有值
        if(page.getRecords().size()>0){
            //获得数据
            List<EduTeacher> items = page.getRecords();
            //组装data
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("total",page.getTotal());
            hashMap.put("rows",page.getRecords());
            return R.ok().data(hashMap);
        }else {
            return R.error();
        }

    }

    @ApiOperation(value = "根据条件分页查询")
    @PostMapping("page/{current}/{pagesize}")
    public R getByWrapperPage(@ApiParam(name = "current",value = "当前页",required = true)
                       @PathVariable("current")Integer current,
                              @ApiParam(name = "pagesize",value = "数据数量",required = true)
                       @PathVariable("pagesize")Integer pagesize,
                              @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
                              @RequestBody TeacherQuery teacherQuery){
        // page分页
        Page<EduTeacher> page=new Page<>(current,pagesize);
        //封装条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //按照时间排序
        wrapper.orderByDesc("gmt_create");
        eduTeacherService.pageQuery(wrapper,teacherQuery);
        //根据条件分页
        IPage<EduTeacher> iPage = eduTeacherService.page(page, wrapper);
        //获得数据
        List<EduTeacher> items = page.getRecords();
        //组装data
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total",page.getTotal());
        hashMap.put("rows",page.getRecords());
        return R.ok().data(hashMap);
    }
    @ApiOperation(value = "保存或者更新讲师")
    @PostMapping("/save")
    public R saveEduTeacher( @ApiParam(name = "eduTeacher", value = "保存|更新讲师对象", required = true)
                             @RequestBody EduTeacher teacher){
        boolean save = eduTeacherService.saveOrUpdate(teacher);
        if(save){
          return R.ok();
        }else {
            return R.error();
        }
    }

}

